﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.Converters
{
    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool visibility;

            if (value.GetType() == typeof(bool))
            {
                visibility = (bool)value;
            }
            else
            {
                decimal numericValue;

                Decimal.TryParse(value.ToString(), out numericValue);

                visibility = numericValue != 0;
            }

            return visibility ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
            return (visibility == Visibility.Visible);
        }
    }
}